<template>
	<view>
		<view class="goods-list">
			<view  v-for="goods in goodsList" :key="goods.cat_id" @click="gotoDetail(goods)">
				<my-goods :goods="goods"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 15
				},
				// 商品列表数据
				goodsList: [],
				// 总数量
				total: 0,
				// 节流阀
				isloading: false
			};
		},
		methods: {		
				// 获取商品列表数据的方法
				async getGoodList(cb) {
					// 打开节流阀
					this.isloading = true
					const res = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
					// 关闭节流阀
					this.isloading = false
					cb && cb()
					if (res.data.meta.status !== 200) {
						return uni.$showMsg()
					}
					this.goodsList = [...this.goodsList, ...res.data.message.goods]
					this.total = res.data.message.total
				},
				// 点击商品项跳转到商品详情
				gotoDetail(goods){
					uni.navigateTo({
						url:'/subpkg/goods-detail/goods-detail?goods_id='+ goods.goods_id
					})
				}
				
			
		},
		// 上拉触底事件函数
		onReachBottom() {
			if(this.queryObj.pagesize * this.queryObj.pagenum >= this.total){
				return uni.$showMsg('数据加载完毕！')
			}
			
			if(this.isloading){
				return
			}
			this.queryObj.pagenum += 1
			this.getGoodList()
		},
		// 下拉刷新事件函数
		onPullDownRefresh(){
			// 重置相关信息
			this.queryObj.pagenum=1
			this.total= 0
			this.isloading=false
			this.goodsList = [],
			// 重新发起请求
			this.getGoodList(()=>{
				// 关闭下拉刷新
				uni.stopPullDownRefresh()
			})
		},

		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''

			this.getGoodList()
		}

	}
</script>

<style lang="scss">

</style>
